package com.lc.rabbit.mq.product;

import com.lc.rabbit.config.RabbitMqConstant;
import com.study.common.enums.DelayedTimeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @description: some desc
 * @author: LChuang
 * @date: 2023/10/26 11:28
 */
@Slf4j
@Component
public class RabbitMqProduct {

    @Resource
    private RabbitTemplate rabbitTemplate;


    public void sendMsg(Map<String, Object> map){
        log.info("【工作模式】发送消息到工作队列成功!");
        rabbitTemplate.convertAndSend(RabbitMqConstant.WORK_DIRECT_EXCHANGE, RabbitMqConstant.WORK_DIRECT_ROUTING, map);
    }

    public void publishMsg(Map<String, Object> map){
        log.info("【订阅模式】发送消息到工作队列成功!");
        // 订阅模式，路由关系传空字符串
        rabbitTemplate.convertAndSend(RabbitMqConstant.FANOUT_EXCHANGE, RabbitMqConstant.FANOUT_DIRECT_ROUTING, map);
    }


    public void deadMsg(Map<String, Object> map){
        log.info("发送延时消息到【延时队列】Delayed01Topic，Delayed02Topic成功！");
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAYED_EXCHANGE, RabbitMqConstant.DELAYED01_ROUTING, map);
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAYED_EXCHANGE, RabbitMqConstant.DELAYED02_ROUTING, map);
    }


    public void delayedMsg(Map<String, Object> map, DelayedTimeEnum timeEnum){
        log.info("发送延时时间：{}ms 的【延时消息】到工作队列成功！", timeEnum.getTime());
        rabbitTemplate.convertAndSend(RabbitMqConstant.DELAYED_EXCHANGE, RabbitMqConstant.DELAYED03_ROUTING, map, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setExpiration(String.valueOf(timeEnum.getTime()));
                return message;
            }
        });
    }


    public void topicMsg(Map<String, Object> map){
        log.info("【通配符模式】发送消息到工作队列成功！");
        rabbitTemplate.convertAndSend(RabbitMqConstant.WILDCARD_EXCHANGE, RabbitMqConstant.WILDCARD01_ROUTING, map);
    }

}
